import {unioformDateTransform} from '../../utils/index.js'
import {handleSql} from '../../database/mysql/index.js'

class thingsModelListModel {
    constructor() {
        this.tableName = 'things_model_list';
    }

    // 根据用户id和角色id查询事项清单
    async getThingsModelListByUserIdAndRoleCode({userId, roleCode}) {   
        const sql = `SELECT * FROM ${this.tableName} WHERE userId = ? AND roleCode = ? `
        let result = await handleSql(sql,[userId,roleCode])
        return result
    }
    
    // 根据id查询事项清单
    async getThingModelById(id){
        const sql = `SELECT * FROM ${this.tableName} WHERE id = ?`
        let result = await handleSql(sql,[id])
        return result[0]
    }

    // 新增事项清单 —— 单条新增
    async addThingsModelList({userInfo, thingsModelData}) {
        const {userId, roleCode} = userInfo;
        const {modelName, theme} = thingsModelData;
        const createDate = unioformDateTransform(new Date())
        const sql = `INSERT INTO ${this.tableName}(userId, roleCode, modelName, theme, createTime) 
                     VALUES (?,?,?,?,?)`
        const result = await handleSql(sql,[userId, roleCode, modelName, theme, createDate])

        if(result.affectedRows == 1){
            return true
        }else{
            return false
        }
    }
}   

export default new thingsModelListModel();